<template>
  <div class="search">
    <form action="/">
      <van-search
        v-model="value"
        show-action
        shape="round"
        autofocus
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
      />
    </form>
    <div class="SearchHistory" v-if="SearchHistory.length>0">
      <div class="sh-top">
        <div class="sh">搜索历史</div>

        <div class="del" @click="onDelete" v-if="isDelete == false">
          <van-icon name="delete-o" size="16" />
        </div>

        <div class="del1" v-if="isDelete">
          <div class="allDelete" @click="allDelete">全部删除</div>
          <div class="i"></div>
          <div class="complete" @click="complete">完成</div>
        </div>
      </div>
      <div class="sh-info">
        <div
          :class="isDelete ? 'd-item' : 'item'"
          v-for="(item, index) in SearchHistory"
          :key="index"
          @click="sear(item)"
        >
          {{ item }}<span class="remove" v-if="isDelete" @click.stop="oneDelete(index)">×</span>
        </div>
      </div>
    </div>
    <div class="commodity">
      <div
        class="bs-item"
        v-for="(item, index) in commodityData"
        :key="index"
        @click="toDerail(item.pid)"
      >
        <span class="hot" v-if="item.isHot == 1">hot</span>
        <img :src="item.largeImg" alt="" />
        <p class="name">{{ item.name }}</p>
        <p class="enname">{{ item.enname }}</p>
        <p class="price">&yen;{{ item.price }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Search",
  data() {
    return {
      isDelete: false,
      value: "",
      commodityData: [],
      SearchHistory: [],
    };
  },
  created() {
    this.getSearchHistory();
  },
  methods: {
    getSearchHistory() {
      let val = localStorage.getItem("value");
      if (val) {
        this.SearchHistory = val.split(",");
      }
    },
    onSearch() {
      if (this.value == "") {
        this.$toast({
          message: "请输入关键字",
        });
        return;
      }
      this.axios({
        method: "get",
        url: "/search",
        params: {
          name: this.value,
        },
      }).then((result) => {
        if (result.data.code == "Q001") {
          this.commodityData = result.data.result;
        }
      });

      if (this.SearchHistory.indexOf(this.value) != -1) {
        return;
      } else {
        this.SearchHistory.push(this.value);
      }
      localStorage.setItem("value", this.SearchHistory);
    },
    onCancel() {
      this.$router.go(-1);
    },
    toDerail(pid) {
      this.$router.push({ name: "Detail", params: { pid } });
    },
    onDelete() {
      this.isDelete = true;
    },
    complete(){
        this.isDelete = false;
    },
    sear(item) {

      this.value = item;
      this.axios({
        method: "get",
        url: "/search",
        params: {
          name: item,
        },
      }).then((result) => {
        if (result.data.code == "Q001") {
          this.commodityData = result.data.result;
        }
      });
    },
    oneDelete(index){
        this.SearchHistory.splice(index,1)
        localStorage.setItem("value", this.SearchHistory);
        if(this.SearchHistory.length == 0){
          this.isDelete = false
        }
    },
    allDelete(){
        this.SearchHistory = []
        localStorage.removeItem('value')
        this.isDelete = false
    }
  },
};
</script>

<style lang="less" scoped>
.van-search{
    padding-top: 40px;
    background-color: #0022ab;
  }
  /deep/.van-nav-bar__title{
    color: #fff;
  }
  /deep/.van-search__action{
    color: #ffff;
  }
.SearchHistory {
  padding: 10px;
  .sh-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .sh {
      font-size: 15px;
      font-weight: 600;
    }
    .del1 {
      display: flex;
      align-items: center;
      .i {
        width: 1px;
        height: 10px;
        background-color: #000;
        margin: 0 5px;
      }
      .complete {
        color: red;
      }
    }
  }
  .sh-info {
    display: flex;
    flex-wrap: wrap;
    .item {
      background-color: #fff;
      margin: 5px;
      padding: 5px 15px;
      border-radius: 15px;
    }
    .d-item {
      background-color: #fff;
      margin: 5px;
      padding: 5px 20px 5px 15px;
      border-radius: 10px;
      position: relative;
      .remove {
        position: absolute;
        top: 1px;
        right: 1px;
        font-size: 22px;
        margin-left: 5px;
        color: #999;
      }
    }
  }
}
.commodity {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  padding: 0 10px;
  .bs-item {
    width: 160px;
    background-color: #fff;
    padding: 10px;
    border-radius: 10px;
    position: relative;
    margin: 8px;
    span {
      background-color: #0022ab;
      color: #fff;
      padding: 5px;
      position: absolute;
      left: 20px;
      border-radius: 0 0 5px 5px;
    }
    img {
      width: 100%;
      border-radius: 8px;
      margin-bottom: 10px;
    }
    .name {
      font-size: 15px;
      color: #666;
      margin-bottom: 5px;
    }
    .enname {
      font-size: 12px;
      color: #999;
      margin-bottom: 10px;
      height: 14px;
      overflow: hidden;
    }
    .price {
      font-size: 15px;
      color: #0022ab;
      font-weight: 600;
    }
  }
}
</style>